package com.billdowney.qq.robot.plugins.provide;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ArrayUtil;
import com.billdowney.qq.robot.annotations.HookMethod;
import com.billdowney.qq.robot.annotations.HookNotice;
import com.billdowney.qq.robot.bean.ConfigBean;
import com.billdowney.qq.robot.plugins.RobotPlugin;
import com.billdowney.qq.robot.plugins.RobotPluginContent;
import com.billdowney.qq.robot.util.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;

/**
 * 打赏插件
 */
@Component
@HookNotice(name = "打赏插件", start = false)
@Slf4j
public class DonateRobotPlugin implements RobotPlugin, InitializingBean {

    @Autowired
    private ConfigBean configBean;
    /**
     * 欢迎图文件夹
     */
    private File donateImgFolder;

    @Override
    public void afterPropertiesSet() throws Exception {
        // 打赏图片文件夹
        this.donateImgFolder = configBean.resolveWorkspace("donate_img");
        FileUtil.mkdir(this.donateImgFolder);
    }

    @HookMethod(equal = "打赏二维码")
    public void deleteGroup(RobotPluginContent content) {
        File[] files = donateImgFolder.listFiles();
        MessageChainBuilder builder = MessageUtil.createBuilder();
        if (ArrayUtil.isEmpty(files)) {
            // 艾特管理员
            configBean.getRootManageQq().stream().map(At::new).forEach(builder::append);
            builder.append(String.format("\n是不是忘记在【%s】里面添加图片了", this.donateImgFolder.getPath()));
        } else {
            Contact contact = content.getContact();
            for (File img : files) {
                builder.append(Objects.requireNonNull(MessageUtil.buildImageMessage(contact, img)));
            }
        }
        content.putReplyMessage(builder);
    }
}
